import React, { Component } from 'react';
import { FormControl, Button, Table, Radio} from 'tinper-bee';
import { addWorkFileList, getManagerList} from './workarchives.config.js';
import './ManagerModalContent.less';
import moment from 'moment';
moment.locale('zh-cn');

export default class ManagerModalContent extends Component {
	constructor (props){
		super(props);
		this.state = {
			data: [],
			text: '',
			selectedRow: [],
			pageNum: 1
		}
	}
	componentDidMount () {
		this.handlerGetSearchManager()
	}
	getColumns = () => {
		let _me = this;
		return [
		 	{ title: "姓名", dataIndex: "userName", key: "userName", width: '20%'},
		 	{ title: "电话", dataIndex: "userMobile", key: "userMobile", width: '20%'},
		 	{ title: "邮箱", dataIndex: "userEmail", key: "userEmail", width: '25%'},
		    { title: "企业", dataIndex: "defaultEntName", key: "defaultEntName", width: '40%'}
	   ]
	}
	onRowClick = (record, index, event)=>{
		let saveManagerRecord = this.props.saveManagerRecord;
		let selectedRow = new Array(this.state.data.length);
        selectedRow[index] = true;
        this.setState({
            selectedRow: selectedRow
        });
		if(saveManagerRecord){
			saveManagerRecord(record)
		}
	}
	handlerSaveManagerText = (e)=>{
		let value = e.target.value;
		this.setState({
			text: value
		});
	}
	handlerGetSearchManager = (initNumFlag) => {
		if (initNumFlag) {
			this.setState({
				pageNum: 1,
				data: []
			},()=>{
				this.handlerGetDataList();
			});
		} else {
			this.handlerGetDataList();
		}
		
	}
	handlerGetDataList = () => {
		let param = {
			keyword: this.state.text,
			pageSize: 200,
			pageNum: this.state.pageNum
		};
		getManagerList(param,
			res=>{
				let data = res.data.result;
				data.content.forEach((item, idnex)=>{
					item['key'] = item['defaultEntId'] + item['userId'];
				})
				this.setState({
					data: [...this.state.data, ...data.content]
				}, ()=>{
					this.setState({
						selectedRow: new Array(this.state.data.length)
					});
				});
			},
			error=>{}
		)
	}
	rowClassNameHandler = (record,index,indent) => {
        if (this.state.selectedRow[index]) {
            return 'selected';
        } else {
            return '';
        }
	}
	handlerSearchMoreManager = () => {
		let pageNum = this.state.pageNum;
		++pageNum;
		this.setState({
			pageNum: pageNum
		}, () => {
			this.handlerGetSearchManager(false);
		});
	}
	render (){
		return (
			<div className="manager--list">
				<div className="list--header clearfix">
					<div className="search-group clearfix">
						<FormControl size="lg" placeholder={`请输入姓名、电话、邮箱、企业名称`} onChange={this.handlerSaveManagerText}/>
						<Button colors="primary" className="search-btn" onClick={()=>this.handlerGetSearchManager(true)}>搜索</Button>
					</div>
				</div>
				<div className="list--content">
					<Table data={this.state.data} 
						   columns={this.getColumns()}
						   onRowClick= {this.onRowClick}
						   rowClassName={this.rowClassNameHandler}
						   scroll={{ y: 300 }}/>
					<a href="javascript:;" className="search-more" onClick={this.handlerSearchMoreManager}>查看更多数据</a>
				</div>
			</div>
			)
	}
}